﻿using B.Z.BasicData.API.Read.Application.Menu;
using B.Z.BasicData.API.Read.DTO;
using B.Z.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.Z.BasicData.API.Read.Controllers
{
    /// <summary>
    /// 控制器-读（菜单）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        private readonly IMediator mediator;

        public MenuController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取菜单下拉框
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<CascaderDTO>>> GetTree()
        {
            MenuCascaderQueryCommand command = new MenuCascaderQueryCommand();
            return mediator.Send(command);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<APIPaging<MenuDto>>> GetMenuList([FromQuery] MenuQueryCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 获取角色拥有的菜单列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<int>>> GetRoleMenu(int rid)
        {
            RoleMenuQueryCommand command = new RoleMenuQueryCommand();
            command.Rid = rid;
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取登录角色拥有的菜单权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<RoleMenuDto>>> GetUserRoleMenu(string ridList)
        {
            MenuRoleQueryCommand command = new MenuRoleQueryCommand();
            command.RidList = ridList.Split(",").Select(x=>Convert.ToInt32(x)).ToList();
            return mediator.Send(command);
        }
    }
}
